package insert;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class insert_fornitore extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String oracleUser = "azienda";
    private String oraclePwd  = "azienda";
    private Connection conn;
    private String driver,oracle;
    private Statement st=null;
    private ResultSet rs=null;
	
    public insert_fornitore() {
        super();
    }
    
    public void init(ServletConfig sc) throws ServletException
    {
    	super.init(sc);
		driver = "oracle.jdbc.driver.OracleDriver";
		oracle = "jdbc:oracle:thin:@localhost:1521:xe";
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			conn = DriverManager.getConnection(oracle, oracleUser, oraclePwd);
			conn.setAutoCommit(false);
			st = conn.createStatement();
			rs = null;
	
			String partitaiva		= "" + request.getParameter("txtCodice");
			String organizzazione 	= "" + request.getParameter("txtNome");
			String tipologia 		= "" + request.getParameter("cmbTipologia");
			String telefono			= "" + request.getParameter("txtTelefono");
			String email			= "" + request.getParameter("txtMail");
			String indirizzo		= "" + request.getParameter("txtIndirizzo");
			String qry				= null;
			
			qry = "insert into fornitori values ("+partitaiva+",'"+organizzazione+"',"+tipologia+",'"+ telefono + "',"
			    + "'" + email + "','" + indirizzo + "')";
			System.out.println("LA QUERY E': "+qry);
			st.executeUpdate(qry);
			
			conn.commit();
			st.close();
			conn.close();
			
			if(request.getSession().getAttribute("type").toString().equalsIgnoreCase("1"))
				response.sendRedirect("adm/ben/select_for.jsp?commit=1");
			else
				response.sendRedirect("amm/ben/select_for.jsp?commit=1");
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
}
